//
//  ResetViewController.m
//  Contacts
//
//  Created by 武 娇 on 14-7-30.
//  Copyright (c) 2014年 李 雷川. All rights reserved.
//

#import "ResetPasswordViewController.h"
#import "Contact.h"
#import "ServerInterface.h"
#import "ContactDao.h"
#import "JGProgressHUD.h"
#import "MainViewController.h"
#import "ResetHeadViewController.h"
extern Contact *globalContact;
@interface ResetPasswordViewController (){
    JGProgressHUD *HUD;
}

@end

@implementation ResetPasswordViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"修改密码";
}

-(void)viewWillAppear:(BOOL)animated{
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Navigation

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender{
    return YES;
}
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
-(IBAction)enterMainViewController:(id)sender{
    NSDictionary *loginDic = @{@"user_name":globalContact.name,@"old_password":@"111111",@"new_password":passwordTextField.text,@"company_id":CompanyID,@"user_id":@(globalContact.userID),@"token":globalContact.token};
    
    HUD = [JGProgressHUD progressHUDWithStyle:JGProgressHUDStyleLight];
    HUD.textLabel.text = @"正在验证";
    [HUD showInView:self.view];
    
    [ServerInterface resetPasswordWithInfoDic:loginDic withBlock:^(NSDictionary *resultDic, NSError *error) {
        if (error) {
            NSLog(@"error is:%@",error);
        }
        else{
            if ([[resultDic valueForKeyPath:@"success"] boolValue]) {
                NSDictionary *dic = [resultDic valueForKeyPath:@"data"];
                [[NSUserDefaults standardUserDefaults]setObject:dic[@"token"] forKey:@"token"];
                globalContact.password = passwordTextField.text;
                globalContact.token = dic[@"token"];
                [self saveToDBWithContact:globalContact];
                [self enterResetHeadViewController];
            }
            else{
                NSString *message =[resultDic objectForKey:@"message"];
                NSLog(@"message is:%@",message);
                HUD.textLabel.text = message;
                sleep(2.0);
            }
        }
        [HUD dismissAfterDelay:2.0];
    }];
}

-(void)enterResetHeadViewController{
    ResetHeadViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ResetHeadViewController"];
    [self.navigationController pushViewController:viewController animated:YES
     ];
}

-(BOOL)checkUser{
    BOOL success = NO;
    return success;

}

-(BOOL)saveToDBWithContact:(Contact *)contact{
    BOOL success = [Contact updateContact:contact];
    return success;
}

@end
